Skip to content

Conversation

@estebank
Copy link
Contributor

@estebank estebank commented Nov 1, 2025

Partially address #37892.

In E0401 generated in resolve:

error[E0401]: can't use generic parameters from outer item
  --> $DIR/E0401.rs:4:39
   |
LL | fn foo<T>(x: T) {
   |        - type parameter from outer item
LL |     fn bfnr<U, V: Baz<U>, W: Fn()>(y: T) {
   |        ----                           ^ use of generic parameter from outer item
   |        |
   |        generic parameter used in this inner function
   |
help: try introducing a local generic parameter here
   |
LL |     fn bfnr<T, U, V: Baz<U>, W: Fn()>(y: T) {
   |             ++

In E0401 generated in hir_typeck:

error[E0401]: can't reference `Self` constructor from outer item
  --> $DIR/do-not-ice-on-note_and_explain.rs:6:13
   |
LL | impl<B> A<B> {
   | ------------ the inner item doesn't inherit generics from this impl, so `Self` is invalid to reference
LL |     fn d() {
LL |         fn d() {
   |            - `Self` used in this inner item
LL |             Self(1)
   |             ^^^^ help: replace `Self` with the actual type: `A`

```
error[E0401]: can't use generic parameters from outer item
  --> $DIR/enum-definition-with-outer-generic-parameter-5997.rs:3:16
   |
LL | fn f<Z>() -> bool {
   |      - type parameter from outer item
LL |     enum E { V(Z) }
   |                ^ use of generic parameter from outer item
   |
help: try introducing a local generic parameter here
   |
LL |     enum E<Z> { V(Z) }
   |           +++
```
```
error[E0401]: can't use generic parameters from outer item
  --> $DIR/E0401.rs:4:39
   |
LL | fn foo<T>(x: T) {
   |        - type parameter from outer item
LL |     fn bfnr<U, V: Baz<U>, W: Fn()>(y: T) {
   |        ----                           ^ use of generic parameter from outer item
   |        |
   |        generic parameter used in this inner function
   |
help: try introducing a local generic parameter here
   |
LL |     fn bfnr<T, U, V: Baz<U>, W: Fn()>(y: T) {
   |             ++
```
```
error[E0401]: can't reference `Self` constructor from outer item
  --> $DIR/do-not-ice-on-note_and_explain.rs:6:13
   |
LL | impl<B> A<B> {
   | ------------ the inner item doesn't inherit generics from this impl, so `Self` is invalid to reference
LL |     fn d() {
LL |         fn d() {
   |            - `Self` used in this inner item
LL |             Self(1)
   |             ^^^^ help: replace `Self` with the actual type: `A`
```
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 1, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 1, 2025

r? @JonathanBrouwer

rustbot has assigned @JonathanBrouwer.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@estebank estebank changed the title Outer param Point at inner item when it uses generic type param from outer item or Self Nov 1, 2025
Comment on lines 12 to 15
help: try introducing a local generic parameter here
|
LL | reuse Trait::static_methodT, {
| ++
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pre-existing mistake with delegation suggestion, we just make it obvious now. This needs to be fixed separately.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there already an issue for this? If not lets make one

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed it in last commit

```
error[E0401]: can't use generic parameters from outer item
  --> $DIR/const-param-from-outer-fn.rs:3:9
   |
LL | fn foo<const X: u32>() {
   |              - const parameter from outer item
LL |     fn bar() -> u32 {
   |        --- generic parameter used in this inner function
LL |         X
   |         ^ use of generic parameter from outer item
   |
help: try introducing a local generic parameter here
   |
LL |     fn bar<X>() -> u32 {
   |           +++
```
Copy link
Contributor

@JonathanBrouwer JonathanBrouwer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few nits, other than that it looks good

View changes since this review

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 2, 2025
@rust-log-analyzer

This comment has been minimized.

@JonathanBrouwer
Copy link
Contributor

r=me when ci is green

@estebank
Copy link
Contributor Author

estebank commented Nov 3, 2025

@bors r=JonathanBrouwer

@bors
Copy link
Collaborator

bors commented Nov 3, 2025

📌 Commit f171c41 has been approved by JonathanBrouwer

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 3, 2025
bors added a commit that referenced this pull request Nov 3, 2025
Rollup of 5 pull requests

Successful merges:

 - #144194 (Provide additional context to errors involving const traits)
 - #148232 (ci: add runners for vanilla LLVM 21)
 - #148240 (rustc_codegen: fix musttail returns for cast/indirect ABIs)
 - #148247 (Remove a special case and move another one out of reachable_non_generics)
 - #148370 (Point at inner item when it uses generic type param from outer item or `Self`)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit c10e1e4 into rust-lang:master Nov 3, 2025
11 checks passed
rust-timer added a commit that referenced this pull request Nov 3, 2025
Rollup merge of #148370 - estebank:outer-param, r=JonathanBrouwer

Point at inner item when it uses generic type param from outer item or `Self`

Partially address #37892.

In E0401 generated in resolve:
```
error[E0401]: can't use generic parameters from outer item
  --> $DIR/E0401.rs:4:39
   |
LL | fn foo<T>(x: T) {
   |        - type parameter from outer item
LL |     fn bfnr<U, V: Baz<U>, W: Fn()>(y: T) {
   |        ----                           ^ use of generic parameter from outer item
   |        |
   |        generic parameter used in this inner function
   |
help: try introducing a local generic parameter here
   |
LL |     fn bfnr<T, U, V: Baz<U>, W: Fn()>(y: T) {
   |             ++
```
In E0401 generated in hir_typeck:
```
error[E0401]: can't reference `Self` constructor from outer item
  --> $DIR/do-not-ice-on-note_and_explain.rs:6:13
   |
LL | impl<B> A<B> {
   | ------------ the inner item doesn't inherit generics from this impl, so `Self` is invalid to reference
LL |     fn d() {
LL |         fn d() {
   |            - `Self` used in this inner item
LL |             Self(1)
   |             ^^^^ help: replace `Self` with the actual type: `A`
```
@rustbot rustbot added this to the 1.93.0 milestone Nov 3, 2025
@estebank estebank mentioned this pull request Nov 3, 2025
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Nov 3, 2025
Tweak E0401

More accurate span pointing at use place of outer param (at ident instead of full path).

Add note explaining why outer item params can't be used in inner item.

Use structured suggestion for what `Self` should have been.

Follow up to rust-lang#148370.

Fix rust-lang#37892.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Nov 3, 2025
Tweak E0401

More accurate span pointing at use place of outer param (at ident instead of full path).

Add note explaining why outer item params can't be used in inner item.

Use structured suggestion for what `Self` should have been.

Follow up to rust-lang#148370.

Fix rust-lang#37892.
rust-timer added a commit that referenced this pull request Nov 4, 2025
Rollup merge of #148447 - estebank:outer-param-2, r=jackh726

Tweak E0401

More accurate span pointing at use place of outer param (at ident instead of full path).

Add note explaining why outer item params can't be used in inner item.

Use structured suggestion for what `Self` should have been.

Follow up to #148370.

Fix #37892.
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Nov 4, 2025
Rollup of 5 pull requests

Successful merges:

 - rust-lang/rust#144194 (Provide additional context to errors involving const traits)
 - rust-lang/rust#148232 (ci: add runners for vanilla LLVM 21)
 - rust-lang/rust#148240 (rustc_codegen: fix musttail returns for cast/indirect ABIs)
 - rust-lang/rust#148247 (Remove a special case and move another one out of reachable_non_generics)
 - rust-lang/rust#148370 (Point at inner item when it uses generic type param from outer item or `Self`)

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants